package com.gklyj.controller.administer;

import com.gklyj.pojo.Position;
import com.gklyj.pojo.Zixun;
import com.gklyj.service.positionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Controller
@Transactional
public class PositionController {

    @Autowired
    positionService positionService;

    /*
    跳转招聘单位岗位信息管理页面
     */
    @GetMapping("/toZpGangweiPage")
    public String toZpGangweiPage(){
        return "administer/zp-position";
    }

    /*
跳转管理员岗位信息管理页面
 */
    @GetMapping("/toAdGangweiPage")
    public String toAdGangweiPage(){
        return "administer/ad-position";
    }

        /*
跳转管理员岗位申请审批页面
 */
    @GetMapping("/toAdGangweiApply")
    public String toAdGangweiApply(){
        return "administer/ad-application";
    }

    /*
    列出所有招聘单位的岗位信息
     */
    @GetMapping("/findListZpGangwei")
    @ResponseBody
    public List<Position> ListZpPosition(){
        return positionService.listPositionsExcludeAdmin();
    }

    /*
    列出所有管理员发布的岗位信息
     */
    @GetMapping("/findListAdGangwei")
    @ResponseBody
    public List<Position> ListAdPosition(){
        return positionService.listPositionsAdmin();
    }


    /*
    管理员更具条件查找招聘单位岗位信息
     */
    @PostMapping("/zp_gangwei_search")
    @ResponseBody
    public List<Position> zp_gangwei_search(@RequestBody Position position){
        return positionService.ZpsearchPositions(position);
    }

    /*
    管理员更具条件查找招聘单位岗位信息
     */
    @PostMapping("/ad_gangwei_search")
    @ResponseBody
    public List<Position> ad_gangwei_search(@RequestBody Position position){
        return positionService.AdsearchPositions(position);
    }

    /*
    批量删除岗位信息
     */
    @PostMapping("/deleteGangweiList")
    @ResponseBody
    public boolean GangweiDeleteList(@RequestBody List<Position> Position) throws RuntimeException{
        Integer sum = Position.size()-1;
        ArrayList<Integer> arrayList=new ArrayList();
        while (sum>=0){
            arrayList.add(Position.get(sum).getG_id());
            sum--;
        }
        return positionService.removeByIds(arrayList);
    }

    /*
    管理员点击详情，显示招聘单位岗位详情信息
     */
    @GetMapping("/toGangweiDetail")
    public String toAdZixunEditPage(@RequestParam("id") String id, Model model){
        Position position = positionService.getById(Integer.valueOf(id));
        model.addAttribute("detail",position);
        return "qiuzhi/qz-gwsq-detail";
    }

    /*
    删除单个岗位信息
     */
    @PostMapping("/deleteOnePosition")
    @ResponseBody
    public boolean deleteOnePosition(@RequestBody Position position) throws RuntimeException{
        return positionService.removeById(position.getG_id());
    }


    /*
    跳转管理员编辑岗位信息页面
     */
    @GetMapping("/toAdPositionEditPage")
    public String toAdPositionEditPage(@RequestParam("id") String id,Model model){
        Position position = positionService.getById(Integer.valueOf(id));
        model.addAttribute("Position",position);
        return "administer/table/ad-gw-edit";
    }

    /*
    管理员编辑岗位信息
     */
    @PostMapping("/editAdPosition")
    @ResponseBody
    public boolean EditAdPosition(@RequestBody Position position) throws RuntimeException{
        return positionService.updateById(position);
    }

    /*
    跳转岗位添加页面
     */
    @GetMapping("/toAdPositionAdd")
    public String toAdPositionAddPage(){
        return "administer/table/ad-gw-add";
    }

    /*
    添加岗位信息
     */
    @PostMapping("/addAdPosition")
    @ResponseBody
    public boolean AddAdPosition(@RequestBody Position position) throws RuntimeException{
        //添加岗位创建时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//定义格式，不显示时分秒
        Timestamp now= new Timestamp(System.currentTimeMillis());//获取系统当前时间
        String str= df.format(now);
        position.setTime(str);
        position.setQuantity(0);
        return positionService.save(position);
    }
}
